package com.xqboss.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xqboss.apps.domain.sys.SysAnnouncementCategory;
import com.xqboss.apps.dto.system.SysAnnouncementCategoryComDto;
import com.xqboss.apps.dto.system.SysAnnouncementCategoryDto;
import com.xqboss.apps.service.sys.SysAnnouncementCategoryService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageResult;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 公告分类表接口
 *
 * @author wangcong
 * @date 2023-08-31
 */
@Api(tags = "公告分类")
@Validated
@RestController
@RequestMapping("/web/announcement/category")
public class WebSysAnnouncementCategoryController extends BaseController {

    @Autowired
    private SysAnnouncementCategoryService sysAnnouncementCategoryService;

    @PreAuthorize("@ss.hasPermi('web:announcement:category:list')")
    @ApiOperation("公告分类列表")
    @GetMapping("list")
    public PageResult<SysAnnouncementCategory> list(@Validated SysAnnouncementCategoryDto<SysAnnouncementCategory> dto) {
        IPage<SysAnnouncementCategory> ipage = sysAnnouncementCategoryService.selectBasicList(dto);
        return PageResult.ok(ipage);
    }

    @PreAuthorize("@ss.hasPermi('web:announcement:category:data')")
    @ApiOperation("公告分类数据（所有数据，创建/编辑公告 时下拉框使用）")
    @GetMapping("data")
    public R<List<SysAnnouncementCategory>> data() {
        List<SysAnnouncementCategory> categories = sysAnnouncementCategoryService.data();
        return R.ok(categories);
    }


    @PreAuthorize("@ss.hasPermi('web:announcement:category:addOrEdit')")
    @ApiOperation("新增/编辑公告分类（编辑传递id）")
    @PostMapping("addOrEdit")
    @Log(title = "公告", businessType = BusinessType.INSERT_OR_UPDATE)
    public R<String> addOrEdit(@RequestBody SysAnnouncementCategory dto) {
        sysAnnouncementCategoryService.saveOrUpdateBasic(dto);
        return R.ok_msg(String.format("公告分类%s成功~", dto.getId() == null ? "新增" : "编辑"));
    }


    @PreAuthorize("@ss.hasPermi('web:announcement:category:del')")
    @ApiOperation("公告分类删除")
    @GetMapping("del/{id}")
    public R<String> del(@PathVariable("id")
                           @ApiParam(value = "公告分类ID", required = true)
                           @NotNull(message = "公告分类ID不能为空")
                           Long id) {
        sysAnnouncementCategoryService.removeBasic(id);
        return R.ok_msg("公告分类删除成功~");
    }


    @PreAuthorize("@ss.hasPermi('web:announcement:category:association')")
    @ApiOperation("公告分类关联公告")
    @PostMapping("association")
    @Log(title = "关联公告", businessType = BusinessType.UPDATE)
    public R<String> association(@RequestBody @Validated SysAnnouncementCategoryComDto dto) {
        sysAnnouncementCategoryService.association(dto.getCategoryId(), dto.getIds());
        return R.ok("关联公告成功~");
    }
}
